package designPattern.singleton;

public class SingletonTest2 {
    public static void main(String[] args) {
        Singleton2 instance1 = Singleton2.getInstance();
        Singleton2 instance2 = Singleton2.getInstance();

        //判断两个对象是否相同
        System.out.println(instance2==instance1);

        System.out.println("instance1.hashCode() = " + instance1.hashCode());
        System.out.println("instance2.hashCode() = " + instance2.hashCode());
    }
}

//饿汉式（静态常量）

class Singleton2{
    //1.私有化构造器
    private Singleton2(){
    }

    //2.本类内部创建对象实例
    private static Singleton2 instance;

    static {//在静态代码块中，创建单例对象
        instance = new Singleton2();
    }


    //3.创建一个内部的静态方法，返回实例对象
    public static Singleton2 getInstance(){
        return instance;
    }
}